package com.baibei.module;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baibei.basic.AppLifecycle;
import com.baibei.model.AddressInfo;
import com.baibei.model.Area;
import com.baibei.model.InoutFundParam;
import com.baibei.model.MergeOrderInfo;
import com.baibei.model.OrderInfo;
import com.baibei.model.ProductInfo;

/**
 * APP路由
 * Created by ChenRui on 2017/5/3 0003 11:22.
 */
public final class AppRouter {

    public static final int REQ_LOGIN = 100;
    public static final int REQ_REGISTER = 101;
    public static final int REQ_RESET_PASSWORD = 102;
    public static final int REQ_ADDRESS = 103; // 地址选择
    public static final int REQ_SETTLEMENT = 104;// 补款取货
    public static final int REQ_LOGIN_BY_ORDERLIST = 105;//登录返回订单列表
    public static final int REQ_LOGIN_BY_WELFARE = 106;//登录返回福利页面
    public static final int REQ_SCAN_CODE = 10234; // 扫描二维码

    public static final String EXTRA_URL = "ROUTER_EXTRA_URL";

    /**
     * form表单 webview使用数据
     */
    public static final String EXTRA_FORM = "ROUTER_EXTRA_FORM";

    /**
     * 欢迎页
     */
    public static final String ROUTER_AREA_LIST = "/home/area";

    /**
     * 新手浮窗引导
     */
    public static final String ROUTE_NEW_HAND = "/guide/newhand";

    /**
     * 欢迎页
     */
    public static final String ROUTER_LAUNCHER = "/launcher/index";

    /**
     * 主界面
     */
    public static final String ROUTER_MAIN = "/ebec/home";

    /**
     * 登录
     */
    public static final String ROUTER_USER_LOGIN = "/login/index";
    /**
     * 注册
     */
    public static final String ROUTER_USER_REGISTER = "/user/register/index";

    /**
     * 注册企业用户
     */
    public static final String ROUTER_USER_REGISTER_ENTERPRISE = "/user/register/enterprise";
    public static final String ROUTER_USER_REGISTER_STATUS = "/user/register/status";

    /**
     * 引导页
     */
    public static final String ROUTER_GUIDE = "/guide/index";

    /**
     * 重置密码
     */
    public static final String ROUTER_USER_RESET_PASSWORD = "/user/reset/password";
    /**
     * 重置资金密码
     * 设置资金密码
     */
    public static final String ROUTER_USER_RESET_PAYMENT_PASSWORD = "/payment/password";


    /**
     * 充值
     */
    public static final String ROUTER_USER_RECHARGE = "/balance/recharge";

    /**
     * 提现
     */
    public static final String ROUTER_USER_WITHDRAW = "/balance/withdraw";

    /**
     * 关于我们
     */
    public static final String ROUTER_ABOUT = "/setting/about";

    /**
     * 账单
     */
    public static final String ROUTER_BILL = "/order/bill";

    /**
     * 订单详情
     */
    public static final String ROUTER_ORDER_DETAIL = "/order/detail";

    /**
     * 福利规则
     */
    public static final String ROUTE_WELFARE_RULE = "/welfare/common/rule";

    /**
     * 福利我的用户
     */
    public static final String ROUTE_WELFARE_MYUSER = "/welfare/common/myuser";

    /**
     * 福利-点点盈利榜
     */
    public static final String ROUTE_WELFARE_RANK = "/welfare/rank/ranking";

    /**
     * 福利-点点圈
     */
    public static final String ROUTE_WELFARE_MOMENTS = "/welfare/moment/home";

    /**
     * 价格浮动设定
     */
    public static final String ROUTE_PRICE_FLOAT = "/priceFloat/float";

    /**
     * 我的跟买
     */
    public static final String ROUTE_MY_FOLLOW = "/follow/myfollow";

    /**
     * 用户详情
     */
    public static final String ROUTE_FOLLOWER_DETAIL = "/follow/detail";

    /**
     * 全屏K线
     */
    public static final String ROUTER_STOCK_KLINE = "/stock/kline";

    /**
     * 初次注册引导
     */
    public static final String ROUTE_FIRST_BUY = "/first/buy";

    /**
     * 网页
     */
    public static final String ROUTER_WEB = "/home/web";
    public static final String ROUTER_INOUTFUND_WEB = "/fund/web";
    public static final String ROUTER_ADDRESS_LIST = "/address/list";
    public static final String ROUTER_ADDRESS_EDIT = "/address/edit";
    public static final String ROUTER_PRODUCT_DETAIL = "/product/detail";
    public static final String ROUTER_PRODUCT_TRADE = "/product/trade";
    public static final String ROUTER_SETTLEMENT = "/settlement/home";
    public static final String ROUTER_SETTLEMENT_STATUS = "/settlement/status";
    public static final String ROUTER_MERGE_ORDERS = "/trade/merge/list";
    public static final String ROUTER_MERGE_ORDER_DETAIL = "/trade/merge/detail";
    public static final String ROUTER_SETTING = "/home/setting";
    public static final String ROUTER_NAME_AUTH = "/user/nameAuth";
    public static final String ROUTER_NAME_AUTH_STATUS = "/user/nameAuthStatus";
    public static final String ROUTER_NOT_ALLOW = "/user/notAllow";
    public static final String ROUTER_FEEDBACK = "/home/feedback";
    public static final String ROUTER_TICKET = "/user/ticket";
    public static final String ROUTER_MESSAGE = "/user/message";


    /**
     * 初始化
     */
    static void init(Application applicationContext) {
        ARouter.init(applicationContext);
    }

    public static void debug() {
        ARouter.openDebug();
        ARouter.openLog();
        ARouter.printStackTrace();
    }



    /**
     * 简单跳转
     *
     * @param path 路由，参考该类静态常量ROUTER_*
     */
    private static void navigation(Context context, String path) {
        ARouter.getInstance().build(path).navigation(context);
    }


    /**
     * 主界面
     */
    public static void routeToMain(Context context) {
        ARouter.getInstance()
                .build(ROUTER_MAIN)
//                .withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);

    }
    public static void routeToMain(Context context,String s){
        ARouter.getInstance().build(ROUTER_MAIN)
                .withString("flag",s)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }
    public static void routeToMain(Context context,int position){
        ARouter.getInstance().build(ROUTER_MAIN)
                .withInt("position",position)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }
    /**
     * 默认登录
     * 登录，登录成功后跳转主界面
     */
    public static void routeToLogin(Activity context) {
        routeToLogin(context, REQ_LOGIN);
//        context.finish();
    }

    /**
     * 新手浮窗引导
     */
    public static void routeToNewhand(Context context){
        ARouter.getInstance()
                .build(ROUTE_NEW_HAND)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }

    /**
     * 跳转到登录界面
     *
     * @param context     context
     * @param requestCode requestCode
     */
    public static void routeToLogin(Activity context, int requestCode) {
        ARouter.getInstance().build(ROUTER_USER_LOGIN)
                .withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)
                .navigation(context, requestCode);
    }

    /**
     * 登录，登录成功后直接返回
     */
    public static void routeToLoginJustReturn(Activity context) {
        ARouter.getInstance().build(ROUTER_USER_LOGIN)
                .withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)
                .withBoolean("justReturn", true)
                .navigation(context, REQ_LOGIN);
    }

    /**
     * 引导页
     */
    public static void routeToGuide(Context context) {
        navigation(context, ROUTER_GUIDE);
    }

    /**
     * 注册
     */
    public static void routeToRegister(Activity context) {
        routeToRegister(context, 0);
    }

    /**
     * 注册
     *
     * @param tab tab索引
     */
    public static void routeToRegister(Activity context, int tab) {
        ARouter.getInstance().build(ROUTER_USER_REGISTER)
                .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .withInt("tab", tab)
                .withTransition(R.anim.activity_slide_in_up, android.R.anim.fade_out)
                .navigation(context, REQ_REGISTER);
    }

    /**
     * 注册
     */
    public static void routeToRegisterEnterprise(Activity context, String mobile, String smsCode, String password) {
        ARouter.getInstance().build(ROUTER_USER_REGISTER_ENTERPRISE)
                .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .withString("mobile", mobile)
                .withString("smsCode", smsCode)
                .withString("password", password)
                .navigation(context, REQ_REGISTER);
    }

    /**
     * 注册失败状态
     */
    public static void routeToRegisterErrorStatus(Context context) {
        ARouter.getInstance().build(ROUTER_USER_REGISTER_STATUS)
                .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .withString("type", "failed")
                .navigation(context);
    }

    /**
     * 注册成功状态
     */
    public static void routeToRegisterSuccessStatus(Context context) {
        ARouter.getInstance().build(ROUTER_USER_REGISTER_STATUS)
                .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .withString("type", "success")
                .navigation(context);
    }

    /**
     * 充值
     */
    public static void routeToRecharge(Context context) {
        ARouter.getInstance().build(ROUTER_USER_RECHARGE).navigation(context);
    }

    /**
     * 出入金网页
     *
     * @param context context
     * @param param   param
     */
    public static void routeToInOutFundWeb(Context context, InoutFundParam param, String title) {
        ARouter.getInstance().build(ROUTER_INOUTFUND_WEB).withParcelable("param", param).withString("title", title).navigation(context);
    }

    /**
     * 跳转充值
     *
     * @param context
     * @param param
     */
    public static void routeRechargeWeb(Context context, InoutFundParam param) {
        routeToInOutFundWeb(context, param, "充值");
    }


    /**
     * 提现
     */
    public static void routeToWithdraw(Context context, String password, InoutFundParam param) {
//        ARouter.getInstance().build(ROUTER_USER_WITHDRAW).withString("password", password).navigation(context);
        routeToInOutFundWeb(context, param, "提现");
    }

    /**
     * 关于我们
     */
    public static void routeToAboutMe(Context context) {
        navigation(context, ROUTER_ABOUT);
    }

    /**
     * 重置登录密码
     */
    public static void routeToResetPassword(Activity context) {
        ARouter.getInstance().build(ROUTER_USER_RESET_PASSWORD).navigation(context, REQ_RESET_PASSWORD);
    }

    /**
     * 重置资金密码
     */
    public static void routeToResPayPwd(Activity context) {
        ARouter.getInstance().build(ROUTER_USER_RESET_PAYMENT_PASSWORD).withInt("type", 1).navigation(context, REQ_RESET_PASSWORD);
    }

    /**
     * 设置资金密码
     */
    public static void routeToSetPayPwd(Activity context) {
        ARouter.getInstance().build(ROUTER_USER_RESET_PAYMENT_PASSWORD).navigation(context, REQ_RESET_PASSWORD);
    }

    /**
     * 账单
     */
    public static void routeToBill(Context context) {
        navigation(context, ROUTER_BILL);
    }


    /**
     * 订单详情跳转
     *
     * @param context   context
     * @param orderInfo info
     */
    public static void routeToOrderDetail(Context context, OrderInfo orderInfo) {
        ARouter.getInstance().build(ROUTER_ORDER_DETAIL).withParcelable("orderInfo", orderInfo).navigation(context);
    }

    /**
     * 订单详情跳转
     *
     * @param context   context
     * @param orderInfo info
     */
    public static void routeToOrderDetailByRecord(Context context, OrderInfo orderInfo) {
        ARouter.getInstance().build(ROUTER_ORDER_DETAIL).withParcelable("orderInfo", orderInfo).withInt("type", 1).navigation(context);
    }


    /**
     * 收货地址
     */
    public static void routeToOrderAddress(Activity context, boolean isSelectMode) {
        ARouter.getInstance().build(ROUTER_ADDRESS_LIST).withBoolean("isSelectAddress", isSelectMode).navigation(context, REQ_ADDRESS);
    }

    public static void routeToOrderAddress(Activity context) {
        routeToOrderAddress(context, false);
    }

    /**
     * 前往订单中心
     *
     * @param context
     */
    public static void routeToOrderCenter(Activity context) {
        ARouter.getInstance().build(ROUTER_MAIN).withFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT).withInt("position", 3).navigation(context);
    }

    /**
     * 跳转网页
     *
     * @param context
     * @param url
     */
    public static void routeToWeb(Context context, String url) {
        routeToWeb(context, null, url);
    }

    /**
     * 跳转网页
     *
     * @param context
     * @param url
     */
    public static void routeToWeb(Context context, String title, String url) {
        ARouter.getInstance().build(ROUTER_WEB).withString(Intent.EXTRA_TITLE, title).withString(EXTRA_URL, url).navigation(context);
    }

    /**
     * 跳转网页
     * html代码注入
     *
     * @param context context
     * @param html    html
     */
    public static void routeToWebByHtml(Context context, String title, String html) {
        ARouter.getInstance().build(ROUTER_WEB).withString(Intent.EXTRA_TITLE, title).withString(EXTRA_FORM, html).navigation(context);
    }


    /**
     * 退出所有的Activity
     */
    public static void finishActivities() {
        AppLifecycle.getDefault().finishAll();
    }

    /**
     * 跳地区列表
     *
     * @param area 地区
     */
    public static void routeToAreaList(Context context, Area area) {
        ARouter.getInstance().build(ROUTER_AREA_LIST).withString("area", area.name()).navigation(context);
    }
//
//    /**
//     * 收货地址列表
//     */
//    public static void routeToAddress(Context context) {
//        ARouter.getInstance().build(ROUTER_ADDRESS_LIST)
//                .navigation(context);
//    }

    /**
     * 收货地址编辑
     *
     * @param addressInfo 地址信息
     * @param isFirst     是否为第一条数据
     */
    public static void routeToEditAddress(Activity context, AddressInfo addressInfo, boolean isFirst) {
        ARouter.getInstance().build(ROUTER_ADDRESS_EDIT)
                .withBoolean("isFirst", isFirst)
                .withParcelable("data", addressInfo)
                .navigation(context, REQ_ADDRESS);
    }

    /**
     * 商品详情
     *
     * @param productInfo 商品信息
     */
    public static void routeToProductDetail(Context context, ProductInfo productInfo) {
        ARouter.getInstance().build(ROUTER_PRODUCT_DETAIL).withParcelable("productInfo", productInfo).navigation(context);
    }

    /**
     * 交易，红酒订购
     *
     * @param estimateTime 到岸时间
     * @param productInfo  产品信息
     */
    public static void routeToTrade(Context context, long estimateTime, ProductInfo productInfo, String saleTips) {
        ARouter.getInstance().build(ROUTER_PRODUCT_TRADE).withLong("estimateTime", estimateTime)
                .withParcelable("productInfo", productInfo)
                .withString("saleTips",saleTips).navigation(context);
    }

    /**
     * 补款取货
     *
     * @param orderInfo 订单信息
     */
    public static void routeToSettlement(Activity context, OrderInfo orderInfo) {
        ARouter.getInstance().build(ROUTER_SETTLEMENT).withParcelable("orderInfo", orderInfo).navigation(context, REQ_SETTLEMENT);
    }

    /**
     * 补款取货状态
     *
     * @param context
     * @param orderInfo 订单信息
     * @param time      补款取货时间
     * @param status    状态
     */
    public static void routeToSettlementStatus(Activity context, OrderInfo orderInfo, String time, String status) {
        ARouter.getInstance().build(ROUTER_SETTLEMENT_STATUS).withParcelable("orderInfo", orderInfo)
                .withString("time", time)
                .withString("status", status)
                .navigation(context, REQ_SETTLEMENT);
    }

    /**
     * 拼单列表
     *
     * @param context
     */
    public static void routeToMergeOrders(Context context) {
        navigation(context, ROUTER_MERGE_ORDERS);
    }

    /**
     * 拼单详情
     *
     * @param mergeOrderInfo 拼单信息
     */
    public static void routeToMergeOrderDetail(Context context, MergeOrderInfo mergeOrderInfo) {
        ARouter.getInstance().build(ROUTER_MERGE_ORDER_DETAIL).withParcelable("mergeOrderInfo", mergeOrderInfo).navigation(context);
    }

    /**
     * 设置
     *
     * @param context
     */
    public static void routeToSetting(Context context) {
        navigation(context, ROUTER_SETTING);
    }

    /**
     * 实名认证
     */
    public static void routeToNameAuth(Context context) {
        navigation(context, ROUTER_NAME_AUTH);
    }

    /**
     * 实名认证成功
     *
     * @param name   姓名，可以为空
     * @param idCard 身份证号码，可以为空
     */
    public static void routeToNameAuthStatus(Context context, String name, String idCard) {
        ARouter.getInstance().build(ROUTER_NAME_AUTH_STATUS)
                .withString("name", name)
                .withString("idCard", idCard)
                .navigation(context);
    }

    /**
     * 功能受限
     */
    public static void routeToNotAllow(Context context) {
        routeToNotAllow(context, null);
    }

    /**
     * 功能受限
     */
    public static void routeToNotAllow(Context context, String title) {
        ARouter.getInstance().build(ROUTER_NOT_ALLOW)
                .withString("title", title)
                .navigation(context);
    }

    /**
     * 意见反馈
     */
    public static void routeToFeedback(Context context) {
        ARouter.getInstance().build(ROUTER_FEEDBACK)
                .navigation(context);
    }

    /**
     * 体验券
     */
    public static void routeToTicket(Context context) {
        ARouter.getInstance().build(ROUTER_TICKET)
                .navigation(context);
    }

    /**
     * 消息中心-带参
     */
    public static void routeToMessage(Context context,String extrakey) {
        ARouter.getInstance().build(ROUTER_MESSAGE)
                .withString("extrakey",extrakey)
                .navigation(context);
    }

    /**
     * 消息中心-无参
     */
    public static void routeToMessage(Context context) {
        ARouter.getInstance().build(ROUTER_MESSAGE)
                .navigation(context);
    }

    /**
     * 福利规则
     */
    public static void routeToWelfRule(Context context){
        ARouter.getInstance()
                .build(ROUTE_WELFARE_RULE)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }

    /**
     * 福利我的用户
     */
    public static void routeToWelfMyuser(Context context){
        ARouter.getInstance()
                .build(ROUTE_WELFARE_MYUSER)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }

    /**
     * 福利-点点盈利榜跳转
     */
    public static void routeToWelfareRank(Context context){
        ARouter.getInstance()
                .build(ROUTE_WELFARE_RANK)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }
    /**
     * 福利-点点圈跳转
     */
    public static void routeToWelfareMoments(Context context){
        ARouter.getInstance()
                .build(ROUTE_WELFARE_MOMENTS)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }

    /**
     * 跳转价格浮动设定
     */
    public static void routeToPriceFloat(Context context,int stairPosition,double close,double last,String name){
        ARouter.getInstance()
                .build(ROUTE_PRICE_FLOAT)
                .withInt("stairPosition",stairPosition)
                .withDouble("close",close)
                .withDouble("last",last)
                .withString("name",name)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }

    /**
     * 跳转我的跟买
     */
    public static void routeToMyFollow(Context context){
        ARouter.getInstance()
                .build(ROUTE_MY_FOLLOW)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }

    /**
     * 跳转用户详情
     */
    public static void routeToFollowerDetail(Context context,String followerId) {
        ARouter.getInstance().build(ROUTE_FOLLOWER_DETAIL)
                .withString("followerId", followerId)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }

    /**
     * 跳转全屏K线
     *
     * @param context
     * @param productInfo   大类的id
     */
    public static void routeToKLine(Context context, String productInfo) {
        ARouter.getInstance().build(ROUTER_STOCK_KLINE).withString("productInfo", productInfo).navigation(context);
    }

    /**
     * 跳转初次购买
     */
    public static void routeToFirstBuy(Context context){
        ARouter.getInstance()
                .build(ROUTE_FIRST_BUY)
                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                .navigation(context);
    }

}
